package com.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 计算录取概率
 * @author zhaoyifan
 * @date 2024/3/5 22:02
 */
public class ProbabilityUtils {

    /**
     * 获取老高考录取概率
     * @param luquWeici
     * @param precedence
     * @param batch 批次
     * @return
     */
    public static Map<String,String> getOldProbability(Integer luquWeici, Integer precedence,String batch){
        Map<String,String> probabilityMap = new HashMap<>();
        if (Objects.nonNull(luquWeici) && luquWeici > 0) {
            Integer x = luquWeici - precedence;
            //如果位次差距大于26000直接默认1%或者99%概率
            if (x > 66000) {
                probabilityMap.put("probability","99%");
                probabilityMap.put("probabilityName","保");
            } else if (x < -66000) {
                probabilityMap.put("probability","1%");
                probabilityMap.put("probabilityName","冲");
            } else {
                BigDecimal f = BigDecimal.valueOf(x).divide(BigDecimal.valueOf(precedence), 4, RoundingMode.HALF_UP);
                BigDecimal f1 = BigDecimal.ZERO;
                if(f.compareTo(new BigDecimal("0")) < 0){
                    f1=new BigDecimal("0.3");
                }else{
                    f1=new BigDecimal("0.75");
                }
                if("本科二批".equals(batch)){
                    if(f.compareTo(new BigDecimal("0")) < 0){
                        f1=new BigDecimal("0.25");
                    }else{
                        f1=new BigDecimal("0.35");
                    }
                }
                if("高职专科".equals(batch)){
                    if(f.compareTo(new BigDecimal("0")) < 0){
                        f1=new BigDecimal("0.25");
                    }else{
                        f1=new BigDecimal("0.3");
                    }
                }

                BigDecimal p = new BigDecimal("0.5").multiply(f.add(f1)).divide(f1,4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
                if (p.compareTo(new BigDecimal(1)) < 0) {
                    probabilityMap.put("probability","1%");
                    probabilityMap.put("probabilityName","冲");
                } else if (p.compareTo(new BigDecimal(99)) > 0) {
                    probabilityMap.put("probability","99%");
                    probabilityMap.put("probabilityName","保");
                } else {
                    if (p.compareTo(new BigDecimal(1)) > 0 && p.compareTo(new BigDecimal(50)) <= 0) {
                        probabilityMap.put("probabilityName","冲");
                    } else if (p.compareTo(new BigDecimal(50)) > 0 && p.compareTo(new BigDecimal(80)) <= 0) {
                        probabilityMap.put("probabilityName","稳");
                    } else if (p.compareTo(new BigDecimal(80)) > 0) {
                        probabilityMap.put("probabilityName","保");
                    }
                    probabilityMap.put("probability",p.setScale(0, RoundingMode.HALF_UP) + "%");
                }
            }
        }
        return probabilityMap;
    }

    /**
     * 获取新高考录取概率
     *
     * @param type       1:物理 2:历史
     * @param luquWeici
     * @param precedence
     * @return
     */
    public static Map<String,String> getProbability(Integer type, Integer luquWeici, Integer precedence){
        Map<String,String> probabilityMap = new HashMap<>();
        if (Objects.nonNull(luquWeici) && luquWeici > 0) {
            Integer x = luquWeici - precedence;
            //如果位次差距大于66000直接默认1%或者99%概率
            if (x > 66000) {
                probabilityMap.put("probability","99%");
                probabilityMap.put("probabilityName","保");
            } else if (x < -66000) {
                probabilityMap.put("probability","1%");
                probabilityMap.put("probabilityName","冲");
            } else {
                BigDecimal f = BigDecimal.valueOf(x).divide(BigDecimal.valueOf(precedence), 4, RoundingMode.HALF_UP);
                BigDecimal f1 = BigDecimal.ZERO;

                if(f.compareTo(new BigDecimal("0")) < 0){
                    if(type == 1){
                        //物理组
                        f1=new BigDecimal("0.3");
                    }else{
                        //历史组
                        f1=new BigDecimal("0.25");
                    }
                }else{
                    if(type == 1){
                        //物理组
                        if(precedence<=60000){
                            f1=new BigDecimal("0.6");
                        }else {
                            f1=new BigDecimal("0.4");
                        }
                    }else{
                        //历史组
                        if(precedence<=14000){
                            f1=new BigDecimal("0.5");
                        }else {
                            f1=new BigDecimal("0.4");
                        }
                    }
                }

                BigDecimal p = new BigDecimal("0.5").multiply(f.add(f1)).divide(f1,4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
                p = p.setScale(0, RoundingMode.HALF_UP);
                if (p.compareTo(new BigDecimal(1)) < 0) {
                    probabilityMap.put("probability","1%");
                    probabilityMap.put("probabilityName","冲");
                } else if (p.compareTo(new BigDecimal(99)) > 0) {
                    probabilityMap.put("probability","99%");
                    probabilityMap.put("probabilityName","保");
                } else {
                    if (p.compareTo(new BigDecimal(1)) >= 0 && p.compareTo(new BigDecimal(50)) <= 0) {
                        probabilityMap.put("probabilityName","冲");
                    } else if (p.compareTo(new BigDecimal(50)) > 0 && p.compareTo(new BigDecimal(80)) <= 0) {
                        probabilityMap.put("probabilityName","稳");
                    } else if (p.compareTo(new BigDecimal(80)) > 0) {
                        probabilityMap.put("probabilityName","保");
                    }
                    probabilityMap.put("probability",p + "%");
                }
            }
        }
        return probabilityMap;
    }
}
